

package com.mt.restaurant.controller.staff;

import org.apache.ibatis.annotations.Param;
import org.springframework.security.access.prepost.PreAuthorize;
import com.mt.common.core.annotation.ApiPageParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.mt.common.core.web.base.PageDTO;
import com.mt.common.core.web.base.PageResultDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import com.mt.restaurant.dto.staff.SalarySettlementEditDto;
import com.mt.restaurant.entity.staff.SalarySettlement;
import com.mt.restaurant.service.staff.SalarySettlementService;
import com.mt.restaurant.service.staff.StaffService;


@Api(tags = "工资结算")
@RestController
@RequestMapping("/api/restaurant/staff/SalarySettlement")
@CrossOrigin(allowCredentials = "true")
public class SalarySettlementController {
    private static Logger logger = LoggerFactory.getLogger(SalarySettlementController.class);


    @Autowired
    private SalarySettlementService salarySettlementService;
    @Autowired
    private StaffService staffService;

    /**
     * 根据分页参数查询工资结算集合
     *
     * @param pageDTO 分页条件
     */
    @PreAuthorize("hasAuthority('restaurant:staff:SalarySettlement:view')")
    @ApiOperation("分页查询工资结算")
    @ApiPageParam
    @PostMapping("/findSalarySettlements")
    public PageResultDTO findSalarySettlements(@RequestBody PageDTO pageDTO) {
        return this.salarySettlementService.findSalarySettlements(pageDTO);
    }

    /**
     * 根据ID查询指定的工资结算
     *
     * @param salarySettlementId Id
     */
    @PreAuthorize("hasAuthority('restaurant:staff:SalarySettlement:view')")
    @ApiOperation("根据id查询工资结算")
    @ApiPageParam
    @PostMapping("/findSalarySettlement")
    public SalarySettlement findSalarySettlement(@RequestParam Long salarySettlementId) {
        return this.salarySettlementService.findSalarySettlement(salarySettlementId);
    }

    /**
     * 根据ID查询指定的工资结算(包含外键名称)
     *
     * @param salarySettlementId Id
     */
    @PreAuthorize("hasAuthority('restaurant:staff:SalarySettlement:view')")
    @ApiOperation("根据ID查询指定的工资结算(包含外键名称)")
    @PostMapping("/findSalarySettlementForView")
    public SalarySettlement findSalarySettlementForView(@RequestParam Long salarySettlementId) {
        return this.salarySettlementService.findSalarySettlementWithForeignName(salarySettlementId);
    }

    /**
     * 根据ID查询指定的工资结算(包含工资结算和外键名称)
     *
     * @param salarySettlementId Id
     */
    @PreAuthorize("hasAuthority('restaurant:staff:SalarySettlement:view')")
    @ApiOperation("根据ID查询指定的工资结算(包含工资结算和外键名称)")
    @PostMapping("/findSalarySettlementForEdit")
    public SalarySettlementEditDto findSalarySettlementForEdit(@RequestParam Long salarySettlementId) {
        SalarySettlementEditDto salarySettlementEditDto = new SalarySettlementEditDto();
        salarySettlementEditDto.setSalarySettlement(this.salarySettlementService.findSalarySettlementWithForeignName(salarySettlementId));

        this.prepareSalarySettlementEditDto(salarySettlementEditDto);

        return salarySettlementEditDto;
    }

    /**
     * 根据ID查询指定的工资结算(只提取ID 和 Name)
     *
     * @param salarySettlementId Id
     */
    @PreAuthorize("hasAuthority('restaurant:staff:SalarySettlement:view')")
    @ApiOperation("根据ID查询指定的工资结算(只提取ID 和 Name)")
    @PostMapping("/findSalarySettlementsWithIdNameById")
    public SalarySettlement findSalarySettlementsWithIdNameById(@RequestParam Long salarySettlementId) {
        return this.salarySettlementService.findSalarySettlementsWithIdNameById(salarySettlementId);
    }

    /**
     * 根据名称查询工资结算集合(只提取ID 和 Name)
     *
     * @param salarySettlementName 名称
     */
    @PreAuthorize("hasAuthority('restaurant:staff:SalarySettlement:view')")
    @ApiOperation("根据名称查询工资结算集合(只提取ID 和 Name)")
    @PostMapping("/findSalarySettlementsWithIdNameByName")
    public List<SalarySettlement> findSalarySettlementsWithIdNameByName(@RequestParam String salarySettlementName) {
        return this.salarySettlementService.findSalarySettlementsWithIdNameByName(salarySettlementName);
    }


    /**
     * 创建新的工资结算
     */
    @PreAuthorize("hasAuthority('restaurant:staff:SalarySettlement:add')")
    @ApiOperation("创建新的工资结算")
    @PostMapping("/createSalarySettlement")
    public SalarySettlementEditDto createSalarySettlement() {
        SalarySettlementEditDto salarySettlementEditDto = new SalarySettlementEditDto();
        salarySettlementEditDto.setSalarySettlement(new SalarySettlement());

        this.prepareSalarySettlementEditDto(salarySettlementEditDto);
        return salarySettlementEditDto;
    }

    private void prepareSalarySettlementEditDto(SalarySettlementEditDto salarySettlementEditDto) {
        //TODO: 以下代码可以注释掉，此行代码即时加载所有外键对象，以便选择。如果不在此加载，可以在界面上实现延迟加载。如果外键对象超过 500 行，建议采用延迟加载
        salarySettlementEditDto.setStaffStaffs(this.staffService.findAllStaffsWithIdName());
    }

    /**
     * 新增保存工资结算
     *
     * @param salarySettlement 实体对象
     */
    @PreAuthorize("hasAuthority('restaurant:staff:SalarySettlement:add')")
    @ApiOperation("新增保存工资结算")
    @PostMapping("/saveSalarySettlement")
    public SalarySettlement saveSalarySettlement(@RequestBody SalarySettlement salarySettlement) {
        return this.salarySettlementService.saveSalarySettlement(salarySettlement);
    }

    /**
     * 修改保存工资结算
     *
     * @param salarySettlement 实体对象
     */
    @PreAuthorize("hasAuthority('restaurant:staff:SalarySettlement:update')")
    @ApiOperation("修改保存工资结算")
    @PostMapping("/updateSalarySettlement")
    public SalarySettlement updateSalarySettlement(@RequestBody SalarySettlement salarySettlement) {
        return this.salarySettlementService.updateSalarySettlement(salarySettlement);
    }

    /**
     * 根据ID删除工资结算
     *
     * @param salarySettlementId ID
     */
    @PreAuthorize("hasAuthority('restaurant:staff:SalarySettlement:remove')")
    @ApiOperation("根据ID删除工资结算")
    @PostMapping("/deleteSalarySettlement")
    public void deleteSalarySettlement(@RequestParam Long salarySettlementId) {
        this.salarySettlementService.deleteSalarySettlement(salarySettlementId);
    }

    @PreAuthorize("hasAuthority('restaurant:staff:SalarySettlement:remove')")
    @ApiOperation("计算工资")
    @PostMapping("/calculateSalary")
    public int calculateSalary(@Param("staffId")Long staffId, @Param("startDate") Date startDate, @Param("endDate")Date endDate) {
       return this.salarySettlementService.calculateSalary(staffId,startDate,endDate);
    }

}

